package com.mzba.easy.weibo.util;

import android.app.Application;
import android.os.Handler;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
/**
 * 
 * @author 06peng
 *
 */
public class CrashApplication extends Application {
	
	public static CrashApplication mDemoApp;
	private ImageCache mImageCache;
	
	private double longitude;
	private double latitude;
	private String address;
	private Handler handler;
	
	public LocationClient mLocationClient = null;
	public MyLocationListenner myListener = new MyLocationListenner();
	
	public static CrashApplication getInstance() {
		return mDemoApp;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		mDemoApp = this;
		CrashHandler crashHandler = CrashHandler.getInstance();
		crashHandler.init(getApplicationContext());
		mImageCache = new ImageCache();
		
		mLocationClient = new LocationClient( this );
		mLocationClient.registerLocationListener( myListener );
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);
		option.setAddrType("detail");
		option.setCoorType("gcj02");
		option.setScanSpan(0);
		option.disableCache(true);//禁止启用缓存定位
		mLocationClient.setLocOption(option);
	}

	@Override
	public void onTerminate() {
		super.onTerminate();
	}

	public ImageCache getImageCache() {
		return mImageCache;
	}
	
	public class MyLocationListenner implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null)
				return ;
			StringBuffer sb = new StringBuffer(256);
			sb.append("time : ");
			sb.append(location.getTime());
			sb.append("\nlatitude : ");
			sb.append(location.getLatitude());
			sb.append("\nlontitude : ");
			sb.append(location.getLongitude());
			if (location.getLocType() == BDLocation.TypeGpsLocation){
			} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
				sb.append("\naddr : ");
				sb.append(location.getAddrStr());
			}
			latitude = location.getLatitude();
			longitude = location.getLongitude();
			address = location.getAddrStr();
			handler.sendEmptyMessage(AppContext.LOCATE_RESULT);
			System.out.println(sb.toString());
		}
		
		public void onReceivePoi(BDLocation poiLocation) {
//			if (poiLocation == null){
//				return ;
//			}
//			StringBuffer sb = new StringBuffer(256);
//			sb.append("Poi time : ");
//			sb.append(poiLocation.getTime());
//			sb.append("\nerror code : ");
//			sb.append(poiLocation.getLocType());
//			sb.append("\nlatitude : ");
//			sb.append(poiLocation.getLatitude());
//			sb.append("\nlontitude : ");
//			sb.append(poiLocation.getLongitude());
//			sb.append("\nradius : ");
//			sb.append(poiLocation.getRadius());
//			if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation){
//				sb.append("\naddr : ");
//				sb.append(poiLocation.getAddrStr());
//			} 
//			if(poiLocation.hasPoi()){
//				sb.append("\nPoi:");
//				sb.append(poiLocation.getPoi());
//			}else{				
//				sb.append("noPoi information");
//			}
//			System.out.println(sb.toString());
		}
	}

	public double getLongitude() {
		return longitude;
	}

	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}

	public double getLatitude() {
		return latitude;
	}

	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}

	public void setHandler(Handler handler) {
		this.handler = handler;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
	
}
